package com.supermap.pisaclient.mine;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.supermap.pisaclient.R;
import com.supermap.pisaclient.common.DividerGridItemDecoration;
import com.supermap.pisaclient.common.SuperMapAplication;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class TestActivity extends Activity {

    List<HashMap<String, Object>> data;
    private GridLayoutManager mLayoutManager;
    Intent intent;
    int[] head_image = new int[]{R.drawable.shop, R.drawable.dynamic, R.drawable.about, R.drawable.app_ico, R.drawable.advert_user_normal, R.drawable.weather_background};
    RecyclerView recyclerview;
    private HomeAdapter mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);

        initData();
        mLayoutManager=new GridLayoutManager(TestActivity.this, 4);//设置为一个4列的纵向网格布局
        recyclerview.addItemDecoration(new DividerGridItemDecoration(this, 4));
        recyclerview.setLayoutManager(mLayoutManager);
        recyclerview.setAdapter(mAdapter = new HomeAdapter());
    }

    private void initData(){

        recyclerview = (RecyclerView) findViewById(R.id.recyclerview);
        data = new ArrayList<>();

        for(int i = 0; i <head_image.length; i++){
            HashMap<String, Object> map1 = new HashMap<String, Object>();
            map1.put("head_image", head_image[i]);
            data.add(map1);
        }
    }

    class HomeAdapter extends RecyclerView.Adapter<HomeAdapter.MyViewHolder> {
        public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            MyViewHolder holder = new MyViewHolder(
                    LayoutInflater.from(TestActivity.this).inflate(R.layout.image_recycle_item,
                    parent, false));
            return holder;
        }

        public void onBindViewHolder(MyViewHolder holder, int position)  {
            int width = getScreenWidth() / 4;
            holder.itemView.setLayoutParams(new LinearLayout.LayoutParams(width, width));
            holder.iv.setImageResource(Integer.parseInt(data.get(position).get("head_image").toString()));
        }

        public int getScreenWidth() {
            WindowManager windowManager = (WindowManager) SuperMapAplication.getContext().getSystemService(Context.WINDOW_SERVICE);
            DisplayMetrics dm = new DisplayMetrics();// 创建了一张白纸
            windowManager.getDefaultDisplay().getMetrics(dm);// 给白纸设置宽高
            return dm.widthPixels;
        }

        @Override
        public int getItemCount() {
            return data.size();
        }

        class MyViewHolder extends RecyclerView.ViewHolder {
            ImageView iv;

            public MyViewHolder(View view) {
                super(view);
                iv = (ImageView) view.findViewById(R.id.recycle_image);
            }
        }
    }
}
